//
//  GalleryHeadView.m
//  WanCaiC8
//
//  Created by tangang on 2018/4/21.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "GalleryHeadView.h"
#import "ImageCell.h"
#import "GalleryDetailVC.h"
@interface GalleryHeadView()<UICollectionViewDelegate,UICollectionViewDataSource,UISearchBarDelegate>
@property (nonatomic ,strong) UICollectionView *collectionView;
@property (nonatomic ,strong) UISearchBar       *searchBar;
@end

@implementation GalleryHeadView{
    QXLable *labTitle;
    UIView *lineView;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        [self addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self);
            make.height.mas_equalTo(@(SCREEN_WIDTH/3.0));
        }];
        
        [self addSubview:self.searchBar];
        [self.searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.top.equalTo(self.collectionView.mas_bottom);
            make.height.mas_equalTo(@44);
        }];
        
        lineView=[[UIView alloc]init];
        lineView.backgroundColor=DEFAULTBGCOLOR;
        [self addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self);
            make.height.mas_equalTo(@5);
        }];
        
        labTitle=[[QXLable alloc]init];
        [self addSubview:labTitle];
        [labTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@30);
            make.right.bottom.equalTo(self);
            make.left.equalTo(self).with.offset(10);
        }];
        
        UIView *line=[[UIView alloc]init];
        line.backgroundColor=DEFAULTBGCOLOR;
        [self addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(line);
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.equalTo(self);
                make.height.mas_equalTo(@.7);
            }];
        }];
    }
    return self;
}

- (void)setTitle:(NSString *)title andShowPic:(BOOL)isShow{
    labTitle.text=title;
    self.collectionView.hidden=!isShow;
    lineView.hidden=isShow;
    self.searchBar.hidden=self.collectionView.hidden;
    
}

- (void)setDataArr:(NSArray *)dataArr{
    _dataArr=dataArr;
    [self.collectionView reloadData];
}

#pragma mark - collectionView代理函数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ImageCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"ImageCell" forIndexPath:indexPath];
    GalleryExtraModel *model=self.dataArr[indexPath.row];
    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.Picture] placeholderImage:[UIImage imageNamed:@"small_loding"]];
    cell.layer.masksToBounds=YES;
    cell.layer.borderColor=NAVCOLOR.CGColor;
    cell.layer.borderWidth=3.0;
    cell.GalleryRecommend.image=[UIImage imageNamed:@"GalleryRecommend"];
    return cell;
    
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 20, 0, 20);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    GalleryExtraModel *model=self.dataArr[indexPath.row];
    GalleryDetailVC *vc=[[GalleryDetailVC alloc]init];
    vc.title=model.Name;
    vc.Id=model.Id;
    UIViewController *selfVC=[TGTools viewController:self];
    vc.saveViewController=selfVC;
    [selfVC.navigationController pushViewController:vc animated:YES];
}

#pragma mark --UISearchBarDelegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    searchBar.showsCancelButton=YES;
    return YES;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [searchBar resignFirstResponder];
    if (searchBar.text>0) {
        if (self.searchBlock) {
            self.searchBlock(searchBar.text);
        }
    }else{
        searchBar.showsCancelButton=NO;
        if (self.cancleBlock) {
            self.cancleBlock();
        }
    }
    
    
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    searchBar.showsCancelButton=NO;
    [searchBar resignFirstResponder];
    if (self.cancleBlock) {
        self.cancleBlock();
    }
}

#pragma mark -- 懒加载
- (UICollectionView *)collectionView{
    if (_collectionView==nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setItemSize:CGSizeMake((SCREEN_WIDTH-80)/3.0, SCREEN_WIDTH/3.0)];
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        layout.minimumInteritemSpacing = 0.0;
        layout.minimumLineSpacing = 0.0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.scrollEnabled=NO;
        _collectionView.backgroundColor=DEFAULTBGCOLOR;
        _collectionView.showsVerticalScrollIndicator=NO;
        [_collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:@"ImageCell"];
    }
    return _collectionView;
}

- (UISearchBar *)searchBar{
    if (!_searchBar) {
        _searchBar=[[UISearchBar alloc]init];
        _searchBar.placeholder=@"搜索";
        _searchBar.delegate=self;
        [_searchBar setBackgroundImage:[TGTools createImageWithColor:DEFAULTBGCOLOR]];
        _searchBar.showsCancelButton=NO;
//        if (@available(iOS 11.0, *)) {
//            [_searchBar setPositionAdjustment:UIOffsetMake((SCREEN_WIDTH-real_size(50))/2, 0) forSearchBarIcon:UISearchBarIconSearch];
//        }
    }
    return _searchBar;
}

@end
